﻿using CarAppClient.Consts;
using CarAppClient.Controls.MessageUsercontrols;
using CarAppClient.Extension;
using CarAppClient.Helpers;
using CarAppClient.ViewModels;
using D.UtilCore.Dtos;
using System.Collections.ObjectModel;
using DelegateCommand = D.WPF.MVVM.EventUtil.DelegateCommand;
using CarAppClient.Views.CarParts.ViewModels.CarParts;
using CarAppDto.CarParts.CarPartsDtos;
using SqlSugar;
using CarAppClient.Views.CarParts;
using System.Linq;
using CarAppClient.Views.Rule.Views;
using CarAppClient.Views.CarType.Views.Brand;
using CarAppClient.Views.CarType.ViewModels.CarType;
using CarAppDto.CarType.BrandDtos;
using CarAppDto.Rule.RuleConfigDtos;
using CarAppDto.CarType.CarTypeDtos;
using CarAppDto.CarType.AreaDtos;
using CarAppDto.CarType.CountryDtos;
using CarAppDto.CarType.VehicleSeriesDtos;
using CarAppDto.Common;

namespace CarAppClient.Views.RuleConfig.ViewModels.RuleConfig
{
    public class RuleConfigModifyViewModel : BaseModifyViewModel<Page_RuleConfig_Modify>
    {      
        #region 加载页面数据

        public RuleConfigModifyViewModel(Page_RuleConfig_Modify owner, string? id, ViewStatus? viewStatus) : base(owner, id, viewStatus)
        {
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="repRuleConfig"></param>
        protected async override void LoadData()
        {
            if (!string.IsNullOrEmpty(Id))
            {
                // 请求后端接口 
                var res = await HttpHelper<RepRuleConfig>.GetAsync($"{ApiUrl.GetRuleConfig}/{Id}");

                //  判断结果
                if (res.Code == StatusCodeEnum.Waring)
                {
                    MsgUtil.Error(res.ErrorMsg);
                }
                else if (res.Code == StatusCodeEnum.Error)
                {
                    MsgUtil.Error("服务器异常");
                }
                else
                {
                    // 绑定数据源到UI 
                    if (res.Data != null)
                    {
                        Name = res.Data.Name;
                        Name = res.Data.Name;
                        No = res.Data.No;
                        Descption = res.Data.Descption;
                        var resBrand = await HttpHelper<RepBrand>.GetAsync($"{ApiUrl.GetBrand}/{res.Data.BrandId}");
                        repBrand = resBrand.Data;
                        Brand = repBrand?.Name;

                        if (res.Data.RuleConfigDetailsList != null)
                            RuleConfigDetailsList.AddRange(res.Data.RuleConfigDetailsList);
                    }
                }
            }
        }
        #endregion

        #region 新增、修改 参数

        RepBrand repBrand = null;
        private string? brand;
        /// <summary>
        /// 品牌
        /// </summary>
        public string? Brand
        {
            get { return brand; }
            set { brand = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 规则名称
        /// </summary>
        public string name;
        public string? Name
        {
            get { return name; }
            set { name = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 规则编码
        /// </summary>
        public string no;
        public string? No
        {
            get { return no; }
            set { no = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 描述
        /// </summary>
        public string descption;
        public string? Descption
        {
            get { return descption; }
            set { descption = value; RaisePropertyChanged(); }
        }

        ObservableCollection<RuleConfigDetails> ruleConfigDetailsList = new ObservableCollection<RuleConfigDetails>();
        public ObservableCollection<RuleConfigDetails> RuleConfigDetailsList
        {
            get { return ruleConfigDetailsList; }
            set { ruleConfigDetailsList = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 保存事件
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="vm"></param>
        protected override async void Submit(object? obj)
        {
         

            //List<CarPartsRuleConfig> CarParts=new List< CarPartsRuleConfig >();
            //repCarPartsList.
            // 基础数据判断
            if (string.IsNullOrEmpty(Id))
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.PostAsync(ApiUrl.AddRuleConfig, new ReqAddRuleConfig()
                {
                    BrandId = repBrand?.Id,
                    Name = Name,
                    No= No,
                    Descption = descption,
                    RuleConfigDetailsList = RuleConfigDetailsList.ToList(),
                }); 

                //  判断结果
                MsgUtil.ShowMsg(res.Code, "新增成功", res.ErrorMsg);
            }
            else
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.PutAsync($"{ApiUrl.UpdateRuleConfig}", new ReqUpdateRuleConfig()
                {
                    Id = Id,
                    BrandId = repBrand?.Id,
                    Name = Name,
                    No = No,
                    Descption = descption,
                    RuleConfigDetailsList = RuleConfigDetailsList.ToList(),
                });
                // 判断结果
                MsgUtil.ShowMsg(res.Code, "修改成功", res.ErrorMsg);
            }
            this.FromClose();
        }
        #endregion


        protected override async void Query(string? type)
        {
        
             if (type == "Brand")
            {
                Page_Brand_Query brand_Query = new Page_Brand_Query();
                ((BrandQueryViewModel)brand_Query.DataContext).InformationRequested += Brand_InformationRequested;
                brand_Query.ShowDialog();
            }
          
        }

        private void Brand_InformationRequested(object sender, InformationEventArgs<RepBrand> e)
        {
            // 处理从ChildViewModel传递过来的信息
            repBrand = e.Information;
            Brand = repBrand?.Name;
        }      

        #region 新增
        /// <summary>
        /// 新增事件
        /// </summary>
        private DelegateCommand? _addCommand;
        /// <summary>
        /// 新增事件
        /// </summary>
        public DelegateCommand AddCommand => _addCommand ?? (_addCommand = new DelegateCommand(Add));
        /// <summary>
        /// 新增
        /// </summary>
        protected  void Add(object? obj) {
            Page_RuleConfigDetails_Modify page_RuleConfigDetails_Modify = new Page_RuleConfigDetails_Modify();
            ((RuleConfigDetailsModifyViewModel)page_RuleConfigDetails_Modify.DataContext).InformationRequested += RuleConfigDetails_InformationRequested;
            page_RuleConfigDetails_Modify.ShowDialog();
        }

        private void RuleConfigDetails_InformationRequested(object sender, InformationEventArgs<RuleConfigDetails> e)
        {
            RuleConfigDetailsList.Add(e.Information);
        }

        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除事件
        /// </summary>
        private DelegateCommand? _batchDeleteCommand;
        /// <summary>
        /// 批量删除事件
        /// </summary>
        public DelegateCommand BatchDeleteCommand => _batchDeleteCommand ?? (_batchDeleteCommand = new DelegateCommand(BatchDelete));
        protected  void BatchDelete(object? obj) {
            RuleConfigDetailsList = new ObservableCollection<RuleConfigDetails>();
            RuleConfigDetailsList.AddRange(((ObservableCollection<RuleConfigDetails>)obj).Where(a => a.IsSelected != true));
        }

        #endregion
    }
}
